package com.autobet.utils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by yangd on 2017-08-27.
 */
public class MapKeyComparator implements Comparator<String> {
    private int sortType = 0;

    public MapKeyComparator(int sortType) {
        this.sortType = sortType;
    }

    public MapKeyComparator() {
        this.sortType = 0;
    }

    public int compare(String str1, String str2) {
        if(this.sortType == 1) {
            String tmp = str1;
            str1 = str2;
            str2 = tmp;
        }

        try {
            Integer istr1 = Integer.valueOf(Integer.parseInt(str1));
            Integer istr2 = Integer.valueOf(Integer.parseInt(str2));
            return istr1.compareTo(istr2);
        } catch (Exception var5) {
            return str1.compareTo(str2);
        }
    }

    public static Map sortMapByKey(Map map, int sortType) {
        if(map != null && !map.isEmpty()) {
            Map sortMap = new TreeMap(new MapKeyComparator(sortType));
            sortMap.putAll(map);
            return sortMap;
        } else {
            return null;
        }
    }
}